﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using QD.Web.Ajax;
using QD.Web.Upload;

namespace QD.Upload
{
    public class UploadHandle:AjaxPage
    {
        protected static IDictionary<String, UploadFileConfig> FileConfigs = new Dictionary<String, UploadFileConfig>(StringComparer.OrdinalIgnoreCase);
        //protected static IDictionary<String, UploadFileConfig> ImageConfigs = new Dictionary<String, UploadFileConfig>(StringComparer.OrdinalIgnoreCase);

        static UploadHandle()
        {
            
            FileConfigs.Add("File", new UploadFileConfig()
            {
                AllowExt = ".jpg;.gif;.png;.jpeg;.doc;.xls;.xlsx;.docx;.rar;.zip;.txt;.pdf",
                MaxFileSize = 1 * 1024 * 1024,
                PathStyle = UploadNameStyle.With_Year_Month,
                RootPath = HttpContext.Current.Server.MapPath("~/Upload"),
                Prefix = "",
                Suffix = "",
                AllowUpload = true
            });


            FileConfigs.Add("Image", new UploadImageConfig()
            {
                AllowExt = ".jpg;.gif;.png;.jpeg;",
                MaxFileSize = 1 * 1024 * 1024,
                PathStyle = UploadNameStyle.With_Year_Month,
                RootPath = HttpContext.Current.Server.MapPath("~/Upload"),
                Prefix = "",
                Suffix = "",
                AllowUpload = true,
                MaxHeight = 600,
                MaxWidth = 800
            });
        }

        protected virtual Boolean IsAuthorize
        {
            get
            {
                return true;
            }
        }

        protected UploadFileConfig CurrentConfig
        {
            get
            {
                String key = Context.Request.QueryString[null];
                return FileConfigs[key];
            }
        }
        protected static void CreateDirectory(string path)
        {
            string parent = path.Substring(0, path.LastIndexOf(Path.DirectorySeparatorChar));
            if (!Directory.Exists(parent))
                CreateDirectory(parent);
            Directory.CreateDirectory(path);
        }
    }
}
